package com.pkx.guide.info.service.impl;

import com.pkx.guide.common.util.StringUtils;
import com.pkx.guide.info.entity.TInfoGuide;
import com.pkx.guide.info.entity.TInfoGuideStatus;
import com.pkx.guide.info.mapper.TInfoGuideMapper;
import com.pkx.guide.info.service.TInfoGuideService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pkx.guide.info.service.TInfoGuideStatusService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 诱导屏信息表 服务实现类
 *
 * @author sunsy
 * @since 2023-11-14
 */
@Service
public class TInfoGuideServiceImpl extends ServiceImpl<TInfoGuideMapper, TInfoGuide> implements TInfoGuideService {

    @Resource
    private TInfoGuideStatusService infoGuideStatusService;

    /**
     * 分页列表
     *
     * @param tInfoGuide
     * @return
     */
    @Override
    public IPage<TInfoGuide> pageList(Page page, TInfoGuide tInfoGuide) {
        LambdaQueryWrapper<TInfoGuide> lambdaQueryWrapper = new QueryWrapper<TInfoGuide>().lambda();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tInfoGuide.getGuidNo()), TInfoGuide::getGuidNo, tInfoGuide.getGuidNo());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tInfoGuide.getGuidName()), TInfoGuide::getGuidName, tInfoGuide.getGuidName());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(tInfoGuide.getDeviceId()), TInfoGuide::getDeviceId, tInfoGuide.getDeviceId());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(tInfoGuide.getGuidIp()), TInfoGuide::getGuidIp, tInfoGuide.getGuidIp());
        lambdaQueryWrapper.eq(tInfoGuide.getGuideType() != null, TInfoGuide::getGuideType, tInfoGuide.getGuideType());
        lambdaQueryWrapper.eq(tInfoGuide.getLaneNo() != null, TInfoGuide::getLaneNo, tInfoGuide.getLaneNo());
        lambdaQueryWrapper.eq(TInfoGuide::getDelFlg, 0);
        IPage<TInfoGuide> resultPage = page(page, lambdaQueryWrapper);
        if (resultPage.getRecords() != null && resultPage.getRecords().size() > 0){
            for (TInfoGuide record : resultPage.getRecords()) {
                TInfoGuideStatus guideStatus = infoGuideStatusService.selectById(record.getId());
                record.setGuideStatus(guideStatus);
            }
        }
        return resultPage;
    }

    /**
     * 列表查询
     *
     * @param tInfoGuide
     * @return
     */
    @Override
    public List<TInfoGuide> listByEntity(TInfoGuide tInfoGuide) {
        LambdaQueryWrapper<TInfoGuide> lambdaQueryWrapper = new QueryWrapper<TInfoGuide>().lambda();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tInfoGuide.getGuidNo()), TInfoGuide::getGuidNo, tInfoGuide.getGuidNo());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tInfoGuide.getGuidName()), TInfoGuide::getGuidName, tInfoGuide.getGuidName());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(tInfoGuide.getDeviceId()), TInfoGuide::getDeviceId, tInfoGuide.getDeviceId());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(tInfoGuide.getGuidIp()), TInfoGuide::getGuidIp, tInfoGuide.getGuidIp());
        lambdaQueryWrapper.eq(tInfoGuide.getGuideType() != null, TInfoGuide::getGuideType, tInfoGuide.getGuideType());
        lambdaQueryWrapper.eq(tInfoGuide.getLaneNo() != null, TInfoGuide::getLaneNo, tInfoGuide.getLaneNo());
        lambdaQueryWrapper.eq(TInfoGuide::getDelFlg, 0);
        List<TInfoGuide> guideList = list(lambdaQueryWrapper);
        if (guideList != null && guideList.size() > 0){
            for (TInfoGuide record : guideList) {
                TInfoGuideStatus guideStatus = infoGuideStatusService.selectById(record.getId());
                record.setGuideStatus(guideStatus);
            }
        }
        return guideList;
    }

    /**
     * 分页查询
     * @param page
     * @param tInfoGuide
     * @return
     */
    @Override
    public IPage<TInfoGuide> pageByBean(Page page, TInfoGuide tInfoGuide){
        return baseMapper.pageByBean(page, tInfoGuide);
    }

    /**
     * 列表查询
     * @param tInfoGuide
     * @return
     */
    @Override
    public List<TInfoGuide> listByBean(TInfoGuide tInfoGuide){
        return baseMapper.listByBean(tInfoGuide);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public TInfoGuide selectById(Integer id) {
        TInfoGuide guide = getById(id);
        if (guide == null){
            throw new RuntimeException("未查询到情报板信息");
        }
        TInfoGuideStatus guideStatus = infoGuideStatusService.selectById(id);
        guide.setGuideStatus(guideStatus);
        return guide;
    }

    /**
     * 保存
     *
     * @param tInfoGuide
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveTInfoGuide(TInfoGuide tInfoGuide) {
        return save(tInfoGuide);
    }

    /**
     * 修改
     *
     * @param tInfoGuide
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modify(TInfoGuide tInfoGuide) {
        return updateById(tInfoGuide);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Integer id) {
        if (id == null){
            return false;
        }
        LambdaUpdateWrapper<TInfoGuide> lambdaUpdateWrapper = new UpdateWrapper<TInfoGuide>().lambda()
            .eq(TInfoGuide::getId, id)
            .set(TInfoGuide::getDelFlg, 1);
        return update(lambdaUpdateWrapper);
    }
}
